package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.EncryptUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.service.ISysAgreementService;
import com.wskj.system.bo.SysAgreementAddBo;
import com.wskj.system.bo.SysAgreementEditBo;
import com.wskj.system.bo.SysAgreementQueryBo;
import com.wskj.system.domain.SysAgreement;
import com.wskj.system.mapper.SysAgreementMapper;
import com.wskj.system.vo.web.SysAgreementVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 协议管理Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Service
public class SysAgreementServiceImpl extends ServicePlusImpl<SysAgreementMapper, SysAgreement> implements ISysAgreementService {

    @Override
    public SysAgreementVo queryById(Long agreementId){
        return getVoById(agreementId, SysAgreementVo.class);
    }

    @Override
    public TableDataInfo<SysAgreementVo> queryPageList(SysAgreementQueryBo bo) {
        PagePlus<SysAgreement, SysAgreementVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysAgreementVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysAgreementVo> queryList(SysAgreementQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysAgreementVo.class);
    }

    private LambdaQueryWrapper<SysAgreement> buildQueryWrapper(SysAgreementQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAgreement> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAgreementTitle()), SysAgreement::getAgreementTitle, bo.getAgreementTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getAgreementType()), SysAgreement::getAgreementType, bo.getAgreementType());
        lqw.eq(StrUtil.isNotBlank(bo.getAgreementContent()), SysAgreement::getAgreementContent, bo.getAgreementContent());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), SysAgreement::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysAgreementAddBo bo) {
        SysAgreement add = BeanUtil.toBean(bo, SysAgreement.class);
        validEntityBeforeSave(add);
        if(Objects.nonNull(bo) && StrUtil.isNotBlank(bo.getReplaceAgreementContent())){
            String content = EncryptUtils.aesDecryptForFront(bo.getReplaceAgreementContent(),EncryptUtils.KEY_DES);
            add.setAgreementContent(content);
        }
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysAgreementEditBo bo) {
        SysAgreement update = BeanUtil.toBean(bo, SysAgreement.class);
        validEntityBeforeSave(update);
        if(Objects.nonNull(bo) && StrUtil.isNotBlank(bo.getReplaceAgreementContent())){
            String content = EncryptUtils.aesDecryptForFront(bo.getReplaceAgreementContent(),EncryptUtils.KEY_DES);
            update.setAgreementContent(content);
        }
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysAgreement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysAgreement(List<SysAgreementVo> sysAgreementList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysAgreementList), "导入数据不能为空");
        int successNum = 0;
        for (SysAgreementVo sysAgreement : sysAgreementList) {
            SysAgreement add = BeanUtil.toBean(sysAgreement, SysAgreement.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysAgreementList.size(), successNum);
        if (successNum != sysAgreementList.size()) {
            msg = msg + String.format("失败 %d 条！", sysAgreementList.size() - successNum);
        }
        return msg;
    }
}
